// pages/我的/index.js
const app=getApp()

Page({
  data: {
    userInfo:{
      username:'',
      avatar:'/image/user_login.png',
      signature:'',
      sex:'',
    },
    noLogin:true,
  },
  onLoad() {
    var that = this
    let storageUserInfo = wx.getStorageSync('userInfo')
    if (storageUserInfo != null && storageUserInfo != '') {
      console.log("login")
      this.setData({
        ['userInfo.username']:storageUserInfo.username,
        ['userInfo.avatar']:storageUserInfo.avatar,
        ['userInfo.sex']:storageUserInfo.sex,
        // avatar:userInfo.avatarUrl,
        noLogin:false
      })
    console.log('() => userInfo: ', that.data.userInfo)
    }
  },

  getuserinfo(){
    var that = this
    wx.getUserProfile({
      desc: '用于完善会员信息',
      success:res =>{
        console.log('用户信息',res.userInfo)
        let storageGender = res.userInfo.gender == 0? '男' : '女'
        this.setData({
          ['userInfo.username']:res.userInfo.nickName,
          ['userInfo.avatar']:res.userInfo.avatarUrl,
          ['userInfo.sex']:storageGender,
          noLogin:false
        }),
        // app.login()
        // wx.setStorageSync('userInfo', that.data.userInfo)
        that.userRegister();
      },
        fail(res){
          console.log('登陆失败',res)
          onLoad:false
        },
      })
    },
    userRegister() {
      var that = this;
      if(app.globalData.user.id == null) {
        console.log("user未注册")
        // 注册到后台数据库
        wx.request({
          url: app.globalData.prefixUrl + '/chong/user/userInfo',
          data:{
              openId:app.globalData.openId,
              avatar:that.data.userInfo.avatar,
              username:that.data.userInfo.username,
              sex:that.data.userInfo.sex,
          },
          method: "POST",
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success(e){
            console.log('用户注册成功: ', e.data)
          }
        })
      } else {
          // user存在, 如果存在那么在onLoad的时候就会set Storage
            // app.globalData.userId = app.globalData.user.id
            // wx.setStorageSync('userInfo', app.globalData.user)
      }
      // 获取用户在数据库中的信息
      // 登录又退出又重登时也要重新setStorage
      wx.request({
        url: app.globalData.prefixUrl+"/chong/wx/"+app.globalData.openId,
        success(e){
          console.log(e)
          app.globalData.user=e.data
          app.globalData.userId = e.data.id
          wx.setStorageSync('userInfo', e.data)
          // app.globalData.token=e.header.Authorization
        },
         fail(e){
          console.log("用户登录失败: ", e)
        }
      })
},
    loginout(){
      this.setData({
        noLogin:true,
        ['userInfo.username']:'please login',
        ['userInfo.avatar']:'/image/user_login.png',
        ['userInfo.sex']:'',
      }),
      wx.setStorageSync('userInfo', null)
    },
  /**
   * 生命周期函数--监听页面加载
   */

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})